import PEDictionary from "../PEDictionary";
import ICfgData, { CfgGame02, CfgGame01, CfgGame03, CfgGame04, CfgGame05, CfgGame06, CfgGame07, CfgGame08, CfgGame09 } from "./ICfgData";
import QuickTool from '../Tool/QuickTool';
import { CfgGame10, CfgGame12, CfgGame14 } from './ICfgData';

const {ccclass, property} = cc._decorator;

@ccclass
export default class CfgMgr {
	private static m_instance: CfgMgr;
	public static get Instance(): CfgMgr {
		if (this.m_instance == null) {
			this.m_instance = new CfgMgr();
		}
		return this.m_instance;
	}
	// private m_allCfgDic: PEDictionary<string, any> = new PEDictionary<string, any>();
	public game01Cfgs: PEDictionary<number, CfgGame01> = new PEDictionary<number, CfgGame01>();
	public game02Cfgs: PEDictionary<number, CfgGame02> = new PEDictionary<number, CfgGame02>();
	public game03Cfgs: PEDictionary<number, CfgGame03> = new PEDictionary<number, CfgGame03>();
	public game04Cfgs: PEDictionary<number, CfgGame04> = new PEDictionary<number, CfgGame04>();
	public game05Cfgs: PEDictionary<number, CfgGame05> = new PEDictionary<number, CfgGame05>();
	public game06Cfgs: PEDictionary<number, CfgGame06> = new PEDictionary<number, CfgGame06>();
	public game07Cfgs: PEDictionary<number, CfgGame07> = new PEDictionary<number, CfgGame07>();
	public game08Cfgs: PEDictionary<number, CfgGame08> = new PEDictionary<number, CfgGame08>();
	public game09Cfgs: PEDictionary<number, CfgGame09> = new PEDictionary<number, CfgGame09>();
	public game10Cfgs: PEDictionary<number, CfgGame10> = new PEDictionary<number, CfgGame10>();
	public game12Cfgs: PEDictionary<number, CfgGame12> = new PEDictionary<number, CfgGame12>();
	public game14Cfgs: PEDictionary<number, CfgGame14> = new PEDictionary<number, CfgGame14>();

	// public loadAllConfig(callBack: () => void, callTarget: any): void {
	//     cc.loader.loadResDir("config", (err: Error, res: any[], urls: string[]) => {
	//         if (err != null) {
	//             console.error(err);
	//         } else {
	//             for (let i = 0; i < res.length; i++) {
	//                 let urlKey = urls[i];

	//                 if (this.m_allCfgDic.ContainsKey(urlKey)) {
	//                     console.error("重复配置" + urlKey);
	//                 } else {
	//                     this.m_allCfgDic.Add(urlKey, res[i]);
	//                 }
	//             }

	//             if (callBack) {
	//                 callBack.apply(callTarget);
	//             }
	//         }
	//     });
	// }

	public cfgs = {
		"01" : {cfg:this.game01Cfgs, cls:CfgGame01},
		"02" : {cfg:this.game02Cfgs, cls:CfgGame02},
		"03" : {cfg:this.game03Cfgs, cls:CfgGame03},
		"04" : {cfg:this.game04Cfgs, cls:CfgGame04},
		"05" : {cfg:this.game05Cfgs, cls:CfgGame05},
		"06" : {cfg:this.game06Cfgs, cls:CfgGame06},
		"07" : {cfg:this.game07Cfgs, cls:CfgGame07},
		"08" : {cfg:this.game08Cfgs, cls:CfgGame08},
		"09" : {cfg:this.game09Cfgs, cls:CfgGame09},
		"10" : {cfg:this.game10Cfgs, cls:CfgGame10},
		"12" : {cfg:this.game12Cfgs, cls:CfgGame12},
		"14" : {cfg:this.game14Cfgs, cls:CfgGame14},
	}

	public async loadOneConfig(gameId: number, callBack: () => void): Promise<void> {
		let idstr = QuickTool.addZero(gameId, 2)

		if (this.cfgs[idstr]){
			await this.loadConfig(`game${idstr}`, "lv", this.cfgs[idstr].cls, this.cfgs[idstr].cfg);
		}

		if (callBack) {
			callBack();
		}
	}


	public async loadAllConfig(callBack: () => void): Promise<void> {
		await this.loadConfig<CfgGame01>("game01", "lv", CfgGame01, this.game01Cfgs);
		await this.loadConfig<CfgGame02>("game02", "lv", CfgGame02, this.game02Cfgs);
		await this.loadConfig<CfgGame03>("game03", "lv", CfgGame03, this.game03Cfgs);
		await this.loadConfig<CfgGame04>("game04", "lv", CfgGame04, this.game04Cfgs);
		await this.loadConfig<CfgGame05>("game05", "lv", CfgGame05, this.game05Cfgs);
		await this.loadConfig<CfgGame06>("game06", "lv", CfgGame06, this.game06Cfgs);
		await this.loadConfig<CfgGame07>("game07", "lv", CfgGame07, this.game07Cfgs);
		await this.loadConfig<CfgGame08>("game08", "lv", CfgGame08, this.game08Cfgs);
		await this.loadConfig<CfgGame09>("game09", "lv", CfgGame09, this.game09Cfgs);

		if (callBack) {
			callBack();
		}
	}

	private loadConfig<T extends ICfgData>(configPath, key:string, ItemClass: {new(): T;}, cfgContainer: PEDictionary<any, any>): Promise<void> {
		return new Promise<void>((resolve, reject) => {
			let cfgPath = "config/" + configPath;
			cc.loader.loadRes(cfgPath, (err: Error, res: cc.JsonAsset) => {
				if (err != null) {
					console.error(err);
				} else {
					for (let i = 0; i < res.json[configPath].length; i++) {
						let oneJsonData: any = res.json[configPath][i];
						let oneItemData = new ItemClass();
						oneItemData.parseData(oneJsonData);
						cfgContainer.Add(oneItemData[key], oneItemData);
					}

					console.log(cfgPath);
					resolve();
				}
			});
		});
	}
}
/**
 * public void NOSomethingA()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingB()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingC()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingD()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingE()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingF()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingG()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingH()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingI()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingJ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingK()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingL()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingM()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingN()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingO()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingP()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingQ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingR()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingS()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingT()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingU()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingV()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
 */